package com.sdry.project.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.common.BaseController;
import com.sdry.common.TableDataInfo;
import com.sdry.exception.BizException;
import com.sdry.project.entity.ProjectEntity;
import com.sdry.project.entity.ProjectExpertEntity;
import com.sdry.project.mapper.ProjectExpertMapper;
import com.sdry.project.service.ProjectExpertService;
import com.sdry.project.service.ProjectService;
import com.sdry.untils.ExcelUtils;
import com.sdry.untils.FileUtil;
import com.sdry.untils.ReadExcelXlsxUtil;
import com.sdry.untils.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenBin
 * @since 2020-08-10
 */
@RestController
@RequestMapping("/project")
public class ProjectController extends BaseController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectExpertMapper projectExpertMapper;

    @Autowired
    private ProjectExpertService projectExpertService;


    @RequestMapping(value = "updateProject")
    public Boolean updateProject(String num,String idea) {
        QueryWrapper<ProjectEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(ProjectEntity::getIdea,idea);
        List<ProjectEntity> list = projectService.list(queryWrapper);
        for (ProjectEntity projectEntity : list) {
            projectEntity.setCompany(num);
            projectService.updateById(projectEntity);
        }
        return true;
    }

    /**
     * 项目分配
     * @param map 参数
     * @return getDataTable
     */
    @RequestMapping(value = "queryPage")
    @ResponseBody
    public TableDataInfo queryPage(@RequestParam HashMap<String,Object> map) {
        PageHelper.startPage(Integer.parseInt(map.get("page").toString()), Integer.parseInt(map.get("limit").toString()));
        QueryWrapper<ProjectEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(ProjectEntity::getPname,map.get("projectName"));
        List<ProjectEntity> list = projectService.list(queryWrapper);
        PageInfo<ProjectEntity> pageInfo = new PageInfo<>(list);
        return getDataTable(list,pageInfo.getTotal());
    }

    @RequestMapping(value = "importDataExcel")
    public Map<String, Object> importDataExcel(MultipartFile file) throws Exception
    {
        try {
            List<ProjectEntity> dataList = getDataList(file);
            projectService.importDataExcel(dataList);
        }catch (Exception e) {
            e.printStackTrace();
            return file(null);
        }

        return file("null");
    }


    public List<ProjectEntity> getDataList(MultipartFile file){
        System.out.println(file.getName()+"\t"+file.getOriginalFilename());
        List<ProjectEntity> dataList = new ArrayList();

        //判断Excel文件是否有内容
        boolean notNull=false;
        String filename=file.getOriginalFilename();
        if (!filename.matches("^.+\\.(?i)(xls)$") && !filename.matches("^.+\\.(?i)(xlsx)$")) {
            throw new BizException("上传文件格式不正确！");
        }
        //判断Excel文件的版本
        boolean isExcel2003=true;
        if (filename.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        Workbook wb = null;
        try {
            InputStream fis=file.getInputStream();
            if (isExcel2003) {
                wb = new HSSFWorkbook(fis);
            } else  {
                wb = new XSSFWorkbook(fis);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 总共有多少张sheet表
        int sheetnum = wb.getNumberOfSheets();

        for (int i = 0; i < sheetnum; i++) {
            Sheet sheet = wb.getSheetAt(i);
            // 表头数据
            Row namerow = sheet.getRow(0);

            // 表头数据
            Row headrow = sheet.getRow(1);

            if (null != headrow) {

                // 总行数
                int rowNum = sheet.getPhysicalNumberOfRows();
                // int rowNum = sheet.getLastRowNum();
                System.out.println(file.getName() + "共：" + rowNum + " 行！");

                // 总列数
                int colNum = headrow.getPhysicalNumberOfCells();

                // 判断工作表是否为空
                if (rowNum == 0) {
                    continue;
                }

                // 循环行
                for (int j = 1; j <= rowNum; j++) {
                    Row row = sheet.getRow(j);
                    if (null != row) {
                        ProjectEntity projectEntity = new ProjectEntity();
                        String name = ReadExcelXlsxUtil.getStringCellvalue(row.getCell(0));


                        String number = "";
                        try {
                            NumberFormat nf = NumberFormat.getInstance();
                            double numericCellValue = row.getCell(1).getNumericCellValue();
                            String s = nf.format(numericCellValue);
                            if (s.indexOf(",") >= 0) {
                                s = s.replace(",", "");
                            }
                            number = s;
                        }catch (Exception e) {
                            number = ReadExcelXlsxUtil.getStringCellvalue(row.getCell(1));
                        }


                        String type = ReadExcelXlsxUtil.getStringCellvalue(row.getCell(2));
                        //String company = ReadExcelXlsxUtil.getStringCellvalue(row.getCell(3));
                        String completeBy = ReadExcelXlsxUtil.getStringCellvalue(row.getCell(3));
                        String completeCompany = ReadExcelXlsxUtil.getStringCellvalue(row.getCell(4));
                        //String people = ReadExcelXlsxUtil.getStringCellvalue(row.getCell(6));

                        String idea = ReadExcelXlsxUtil.getStringCellvalue(row.getCell(5));

                        String url = ReadExcelXlsxUtil.getStringCellvalue(row.getCell(6));
                        String ppt = ReadExcelXlsxUtil.getStringCellvalue(row.getCell(7));

                        projectEntity.setPnumber(number);
                        projectEntity.setPname(name);
                        projectEntity.setType(type);
                        //projectEntity.setCompany(company);
                        projectEntity.setCompleteCompany(completeCompany);
                        projectEntity.setCompleteBy(completeBy);
                        projectEntity.setUrl(url);
                        //projectEntity.setPeople(people);
                        projectEntity.setPpt(ppt);
                        projectEntity.setIdea(idea);
                        dataList.add(projectEntity);

                        //continue;
                    }
                }
            }
        }

        return dataList;
    }

    /**
     * 返回是否上传成功的参数
     * @param url
     * @return
     */
    public Map<String,Object> file(String url){
        Map<String,Object> map = new HashMap(16);
        if (url == null||url == "") {
            map.put("msg","上传失败");
            map.put("code",500);
            return map;
        }
        map.put("msg","上传成功");
        map.put("code",200);
        map.put("url",url);
        return map;
    }

    public static Object getCellFormatValue(Cell cell){
        Object cellValue = null;
        if(cell!=null){
            //判断cell类型
            switch(cell.getCellType()){
                case Cell.CELL_TYPE_NUMERIC:{
                    cellValue = String.valueOf(cell.getNumericCellValue());
                    break;
                }
                case Cell.CELL_TYPE_FORMULA:{
                    //判断cell是否为日期格式
                    if(DateUtil.isCellDateFormatted(cell)){
                        //转换为日期格式YYYY-mm-dd
                        cellValue = cell.getDateCellValue();
                    }else{
                        //数字
                        cellValue = String.valueOf(cell.getNumericCellValue());
                    }
                    break;
                }
                case Cell.CELL_TYPE_STRING:{
                    cellValue = cell.getRichStringCellValue().getString();
                    break;
                }
                default:
                    cellValue = "";
            }
        }else{
            cellValue = "";
        }
        return cellValue;
    }
    /**
     * 导出excel
     */
    @RequestMapping("exportExcel4")
    public void exportExcel4(String type,String[] projectId,String exportName,String idea,HttpServletResponse response) throws UnsupportedEncodingException {
        idea = "人物奖";
        //头信息
        List<String> list = new ArrayList<>();
        list.add("序号");
        list.add("组别");
        list.add("姓名");
        list.add("推荐单位");
        list.add("性别");
        list.add("出生年月");
        list.add("组别");
        list.add("评定等级");
        //list.add("平均分");
        //list.add("原等级");
        List<List<String>> patents = new ArrayList<>();
        QueryWrapper<ProjectEntity> queryWrapper = new QueryWrapper<>();
        List<ProjectEntity> list1 = new ArrayList<>(16);
        if ("0".equals(type)) {
            //导出全部
            List<ProjectEntity> list2 = projectService.list(queryWrapper);

            List<ProjectEntity> listNew1  = new ArrayList<>();
            List<ProjectEntity> listNew4  = new ArrayList<>();
            List<ProjectEntity> listNew7  = new ArrayList<>();
            List<ProjectEntity> listNew8  = new ArrayList<>();
            List<ProjectEntity> listNew  = new ArrayList<>();
            for (ProjectEntity projectEntity : list2) {
                if ("1".equals(projectEntity.getAwardsresult())) {
                    listNew1.add(projectEntity);
                }else if ("4".equals(projectEntity.getAwardsresult())) {
                    listNew4.add(projectEntity);
                }else if ("7".equals(projectEntity.getAwardsresult())) {
                    listNew7.add(projectEntity);
                }else if (StringUtil.isEmpty(projectEntity.getAwardsresult()) && StringUtil.isNotEmpty(projectEntity.getIdea())) {
                    if ("一等奖".equals(projectEntity.getIdea())) {
                        listNew1.add(projectEntity);
                    }else if ("二等奖".equals(projectEntity.getIdea())) {
                        listNew4.add(projectEntity);
                    }else if ("三等奖".equals(projectEntity.getIdea())) {
                        listNew7.add(projectEntity);
                    }else if ("人物奖".equals(projectEntity.getIdea())) {
                        listNew8.add(projectEntity);
                    }
                }else if ("0".equals(projectEntity.getAwardsresult()) && StringUtil.isNotEmpty(projectEntity.getIdea())) {
                    if ("一等奖".equals(projectEntity.getIdea())) {
                        listNew1.add(projectEntity);
                    }else if ("二等奖".equals(projectEntity.getIdea())) {
                        listNew4.add(projectEntity);
                    }else if ("三等奖".equals(projectEntity.getIdea())) {
                        listNew7.add(projectEntity);
                    }else if ("人物奖".equals(projectEntity.getIdea())) {
                        listNew8.add(projectEntity);
                    }
                }
            }
            if ("一等奖".equals(idea)) {
                listNew.addAll(listNew1);
            }else if ("二等奖".equals(idea)) {
                listNew.addAll(listNew4);
            }else if ("三等奖".equals(idea)) {
                listNew.addAll(listNew7);
            }else if ("人物奖".equals(idea)) {
                listNew.addAll(listNew8);
            }
            list1 = listNew;
        }if ("1".equals(type)) {
            for (String strId : projectId) {
                ProjectEntity projectEntity = projectService.getById(strId);
                list1.add(projectEntity);
            }
        }
        if (list1.size() == 0 || list1 == null) {
            list1 = new ArrayList<>();
        }else {
            for (ProjectEntity projectEntity : list1) {
                List<String> stringList = new ArrayList<>();
                //项目编号
                stringList.add(projectEntity.getPnumber());
                //组别
                stringList.add(projectEntity.getType());
                //姓名
                stringList.add(projectEntity.getPname());
                //推荐单位
                stringList.add(projectEntity.getSellUnit());
                //性别
                stringList.add(projectEntity.getSex());
                //出生年月
                stringList.add(projectEntity.getBirthDate());
                //组别
                stringList.add(projectEntity.getTeam());
                //获奖等级
                String str = getString2(projectEntity.getAwardsresult(),projectEntity.getIdea());
                stringList.add(str);
                // 平均分

                //String scoringNew = getStringScoring(projectEntity.getId());
                //stringList.add(scoringNew);
                // 封装数据
                patents.add(stringList);
            }
        }
        ExcelUtils.exportFile(exportName, list, patents, response);
    }

    @RequestMapping("/project/exportExcel")
    public void exportExcel(String exportName,String type,HttpServletResponse response) throws UnsupportedEncodingException {
        QueryWrapper<ProjectEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(ProjectEntity::getPname,map.get("projectName"));
        List<ProjectEntity> list = projectService.listByExpoer2021(map.get("projectName"),null);
        for (ProjectEntity projectEntity : list) {
            String realNames = projectExpertService.listUserRelName(projectEntity.getId());
            projectEntity.setExpert(realNames);
        }
        List<ProjectEntity> collect = new ArrayList<>();
        if ("0".equals(type)) {
            collect = list.stream().sorted(Comparator.comparing(ProjectEntity::getIdea)).collect(Collectors.toList());
        }else if ("1".equals(type)) {
            collect = list.stream().filter(item->"一等奖".equals(item.getIdea())).collect(Collectors.toList());
        }else if ("2".equals(type)) {
            collect = list.stream().filter(item->"二等奖".equals(item.getIdea())).collect(Collectors.toList());
        }else if ("3".equals(type)) {
            collect = list.stream().filter(item->"三等奖".equals(item.getIdea())).collect(Collectors.toList());
        }
        // 排序，按照推荐等级
        List<List<String>> datas = new ArrayList<>();
        for (ProjectEntity projectEntity : collect) {
            List<String> stringList = new ArrayList<>();
            //项目编号
            stringList.add(projectEntity.getPnumber());
            //项目名称
            stringList.add(projectEntity.getPname());
            //类型
            stringList.add(projectEntity.getType());
            //完成单位
            stringList.add(projectEntity.getCompleteCompany());
            //完成人
            stringList.add(projectEntity.getCompleteBy());
            //推荐等级
            stringList.add(projectEntity.getIdea());
            // 总票数
            stringList.add(projectEntity.getTotal());
            // 同意票数
            stringList.add(projectEntity.getEvaluationOpinion());
            // 不同意票数
            stringList.add(projectEntity.getNotAgree());
            // 二次同意票数
            stringList.add(projectEntity.getEvaluationOpinionNext());
            datas.add(stringList);
        }

        List<String> titleList = new ArrayList<>();
        titleList.add("项目序号");
        titleList.add("项目名称");
        titleList.add("类型");
        titleList.add("完成单位");
        titleList.add("完成人");
        titleList.add("推荐等级");
        titleList.add("总票数");
        titleList.add("同意票数");
        titleList.add("不同意票数");
        titleList.add("二次同意票数");

        ExcelUtils.exportFile(exportName, titleList, datas, response);
    }

    /**
     * 导出excel
     */
    @RequestMapping("exportExcel")
    public void exportExcel(String type,String[] projectId,String exportName,String idea,HttpServletResponse response) throws UnsupportedEncodingException {
        idea = "一等奖";
        //头信息
        List<String> list = new ArrayList<>();
        list.add("项目序号");
        list.add("项目名称");
        list.add("类型");
        list.add("完成单位");
        list.add("完成人");
        list.add("评定等级");
        //list.add("平均分");
        //list.add("原等级");
        List<List<String>> patents = new ArrayList<>();
        QueryWrapper<ProjectEntity> queryWrapper = new QueryWrapper<>();
        List<ProjectEntity> list1 = new ArrayList<>(16);
        if ("0".equals(type)) {
            //导出全部
            List<ProjectEntity> list2 = projectService.list(queryWrapper);

            List<ProjectEntity> listNew1  = new ArrayList<>();
            List<ProjectEntity> listNew4  = new ArrayList<>();
            List<ProjectEntity> listNew7  = new ArrayList<>();
            List<ProjectEntity> listNew  = new ArrayList<>();
            for (ProjectEntity projectEntity : list2) {
                if ("1".equals(projectEntity.getAwardsresult())) {
                    listNew1.add(projectEntity);
                }else if ("4".equals(projectEntity.getAwardsresult())) {
                    listNew4.add(projectEntity);
                }else if ("7".equals(projectEntity.getAwardsresult())) {
                    listNew7.add(projectEntity);
                }else if (StringUtil.isEmpty(projectEntity.getAwardsresult()) && StringUtil.isNotEmpty(projectEntity.getIdea())) {
                    if ("一等奖".equals(projectEntity.getIdea())) {
                        listNew1.add(projectEntity);
                    }else if ("二等奖".equals(projectEntity.getIdea())) {
                        listNew4.add(projectEntity);
                    }else if ("三等奖".equals(projectEntity.getIdea())) {
                        listNew7.add(projectEntity);
                    }
                }else if ("0".equals(projectEntity.getAwardsresult()) && StringUtil.isNotEmpty(projectEntity.getIdea())) {
                    if ("一等奖".equals(projectEntity.getIdea())) {
                        listNew1.add(projectEntity);
                    }else if ("二等奖".equals(projectEntity.getIdea())) {
                        listNew4.add(projectEntity);
                    }else if ("三等奖".equals(projectEntity.getIdea())) {
                        listNew7.add(projectEntity);
                    }
                }
            }
            if ("一等奖".equals(idea)) {
                listNew.addAll(listNew1);
            }else if ("二等奖".equals(idea)) {
                listNew.addAll(listNew4);
            }else if ("三等奖".equals(idea)) {
                listNew.addAll(listNew7);
            }
            list1 = listNew;
        }if ("1".equals(type)) {
            for (String strId : projectId) {
                ProjectEntity projectEntity = projectService.getById(strId);
                list1.add(projectEntity);
            }
        }
        if (list1.size() == 0 || list1 == null) {
            list1 = new ArrayList<>();
        }else {
            for (ProjectEntity projectEntity : list1) {
                List<String> stringList = new ArrayList<>();
                //项目编号
                stringList.add(projectEntity.getPnumber());
                //项目名称
                stringList.add(projectEntity.getPname());
                //类型
                stringList.add(projectEntity.getType());
                //完成单位
                stringList.add(projectEntity.getCompleteCompany());
                //完成人
                stringList.add(projectEntity.getCompleteBy());
                //获奖等级
                String str = getString2(projectEntity.getAwardsresult(),projectEntity.getIdea());
                stringList.add(str);
                // 平均分

                //String scoringNew = getStringScoring(projectEntity.getId());
                //stringList.add(scoringNew);
                // 封装数据
                patents.add(stringList);
            }
        }
        ExcelUtils.exportFile(exportName, list, patents, response);
    }



    /**
     * 导出excel
     */
    @RequestMapping("exportExcel2")
    public void exportExcel2(String type,String[] projectId,String exportName,String idea,HttpServletResponse response) throws UnsupportedEncodingException {
        //头信息
        List<String> list = new ArrayList<>();
        list.add("项目序号");
        list.add("项目名称");
        list.add("类型");
        list.add("完成单位");
        list.add("完成人");
        list.add("评定等级");
        //list.add("平均分");
        //list.add("原等级");
        List<List<String>> patents = new ArrayList<>();
        QueryWrapper<ProjectEntity> queryWrapper = new QueryWrapper<>();
        List<ProjectEntity> list1 = new ArrayList<>(16);
        if ("0".equals(type)) {
            //导出全部
            List<ProjectEntity> list2 = projectService.list(queryWrapper);

            List<ProjectEntity> listNew1  = new ArrayList<>();
            List<ProjectEntity> listNew4  = new ArrayList<>();
            List<ProjectEntity> listNew  = new ArrayList<>();
            for (ProjectEntity projectEntity : list2) {
                if ("1".equals(projectEntity.getAwardsresult())) {
                    listNew1.add(projectEntity);
                }else if ("4".equals(projectEntity.getAwardsresult())) {
                    listNew4.add(projectEntity);
                }else if (StringUtil.isEmpty(projectEntity.getAwardsresult()) && StringUtil.isNotEmpty(projectEntity.getIdea())) {
                    if ("一等奖".equals(projectEntity.getIdea())) {
                        listNew1.add(projectEntity);
                    }else if ("二等奖".equals(projectEntity.getIdea())) {
                        listNew4.add(projectEntity);
                    }
                }else if ("0".equals(projectEntity.getAwardsresult()) && StringUtil.isNotEmpty(projectEntity.getIdea())) {
                    if ("一等奖".equals(projectEntity.getIdea())) {
                        listNew1.add(projectEntity);
                    }else if ("二等奖".equals(projectEntity.getIdea())) {
                        listNew4.add(projectEntity);
                    }
                }
            }
            if ("一等奖".equals(idea)) {
                listNew.addAll(listNew1);
            }else if ("二等奖".equals(idea)) {
                listNew.addAll(listNew4);
            }
            list1 = listNew;
        }if ("1".equals(type)) {
            for (String strId : projectId) {
                ProjectEntity projectEntity = projectService.getById(strId);
                list1.add(projectEntity);
            }
        }
        if (list1.size() == 0 || list1 == null) {
            list1 = new ArrayList<>();
        }else {
            for (ProjectEntity projectEntity : list1) {
                List<String> stringList = new ArrayList<>();
                //项目编号
                stringList.add(projectEntity.getPnumber());
                //项目名称
                stringList.add(projectEntity.getPname());
                //类型
                stringList.add(projectEntity.getType());
                //完成单位
                stringList.add(projectEntity.getCompleteCompany());
                //完成人
                stringList.add(projectEntity.getCompleteBy());
                //获奖等级
                String str = getString2(projectEntity.getAwardsresult(),projectEntity.getIdea());
                stringList.add(str);
                // 平均分
                //String scoringNew = getStringScoring(projectEntity.getId());
                //stringList.add(scoringNew);
                // 封装数据
                patents.add(stringList);
            }
        }
        ExcelUtils.exportFile(exportName, list, patents, response);
    }



    public  String getStringScoring(Long projectId) {
        QueryWrapper<ProjectExpertEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(ProjectExpertEntity::getProjectId,projectId);
        List<ProjectExpertEntity> list2 = projectExpertMapper.selectList(queryWrapper1);
        Integer scoring = 0;
        for (ProjectExpertEntity projectExpertEntity : list2) {
            if (StringUtil.isNotEmpty(projectExpertEntity.getScoring())) {
                scoring += Integer.parseInt(projectExpertEntity.getScoring());
            }
        }
        if (list2.size() == 0) return "";
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format((float)scoring / list2.size());
    }

    private String getString2(String awardsresult, String idea) {
        String result = "";
        String string = getString(awardsresult);
        if (StringUtil.isNotEmpty(idea) && StringUtil.isNotEmpty(string)) {
            if (idea.equals(string)) {
                return string;
            }else if ("一等奖".equals(idea) && "二等奖".equals(string)){
                return "一等奖降二等奖";
            }else if ("二等奖".equals(idea) && "三等奖".equals(string)){
                return "二等奖降三等奖";
            }
        }
        return idea;
    }

    @NotNull
    private String getString(String primaryAwardsResult) {
        String strResult = "";
        if ("1".equals(primaryAwardsResult)) {
            strResult = "一等奖";
        }else if ("2".equals(primaryAwardsResult)) {
            strResult = "一等奖B";
        }else if ("3".equals(primaryAwardsResult)) {
            strResult = "一等奖C";
        }else if ("4".equals(primaryAwardsResult)) {
            strResult = "二等奖";
        }else if ("5".equals(primaryAwardsResult)) {
            strResult = "二等奖B";
        }else if ("6".equals(primaryAwardsResult)) {
            strResult = "二等奖C";
        }else if ("7".equals(primaryAwardsResult)) {
            strResult = "三等奖";
        }else if ("8".equals(primaryAwardsResult)) {
            strResult = "三等奖B";
        }else if ("9".equals(primaryAwardsResult)) {
            strResult = "三等奖C";
        }else if ("10".equals(primaryAwardsResult)) {
            strResult = "一等奖降二等奖";
        }else if ("11".equals(primaryAwardsResult)) {
            strResult = "二等奖降三等奖";
        } else if ("0".equals(primaryAwardsResult)) {
            strResult = "未授奖";
        } else {
            strResult = "";
        }
        return strResult;
    }

    /*@NotNull
    private String getString(String primaryAwardsResult) {
        String strResult = "";
        if ("1".equals(primaryAwardsResult)) {
            strResult = "一等奖A";
        } else if ("2".equals(primaryAwardsResult)) {
            strResult = "一等奖B";
        } else if ("3".equals(primaryAwardsResult)) {
            strResult = "一等奖C";
        } else if ("4".equals(primaryAwardsResult)) {
            strResult = "二等奖A";
        } else if ("5".equals(primaryAwardsResult)) {
            strResult = "二等奖B";
        } else if ("6".equals(primaryAwardsResult)) {
            strResult = "二等奖C";
        } else if ("7".equals(primaryAwardsResult)) {
            strResult = "三等奖A";
        } else if ("8".equals(primaryAwardsResult)) {
            strResult = "三等奖B";
        } else if ("9".equals(primaryAwardsResult)) {
            strResult = "三等奖C";
        } else if ("0".equals(primaryAwardsResult)) {
            strResult = "未授奖";
        } else {
            strResult = "";
        }
        return strResult;
    }*/

    @RequestMapping("/download")
    public void downloadFile(HttpServletResponse response,String type) throws IOException {
        try {
            String  str = "";
            String fileName = null;
            if ("project".equals(type)) {
                str = "项目";
                fileName = "项目导入模板.xlsx";
            }else if ("user".equals(type)){
                str = "专家";
                fileName = "专家导入模板.xlsx";
            }

            Resource resource = new ClassPathResource("/static/excel/"+str+"导入模板.xlsx");
            File sourceFile =  resource.getFile();
            if (!sourceFile.exists()) {
                throw new BizException("模板文件不存在");
            }
            // 文件的默认保存名

            InputStream inputStream = new FileInputStream(sourceFile);
            //强制下载不打开
            response.setContentType("application/force-download");
            OutputStream out = response.getOutputStream();
            //使用URLEncoder来防止文件名乱码或者读取错误
            assert fileName != null;
            response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName, "UTF-8"));
            int b = 0;
            byte[] buffer = new byte[1000000];
            while (b != -1) {
                b = inputStream.read(buffer);
                if(b!=-1) {
                    out.write(buffer, 0, b);
                }
            }
            inputStream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 下载项目简介（ppt格式）
     * @param response 响应
     * @param fileName 文件名
     * @throws IOException
     */
    @RequestMapping("/introduction")
    public void introduction(HttpServletResponse response, String fileName ) throws IOException {
        try {
            Resource resource = new ClassPathResource("/static/ppt/"+fileName);
            File sourceFile =  resource.getFile();
            if (!sourceFile.exists()) {
                throw new BizException("文件不存在");
            }
            // 文件的默认保存名
            InputStream inputStream = new FileInputStream(sourceFile);
            //强制下载不打开
            response.setContentType("application/force-download");
            OutputStream out = response.getOutputStream();
            //使用URLEncoder来防止文件名乱码或者读取错误
            assert fileName != null;
            response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName, "UTF-8"));
            int b = 0;
            byte[] buffer = new byte[1000000];
            while (b != -1) {
                b = inputStream.read(buffer);
                if(b!=-1) {
                    out.write(buffer, 0, b);
                }
            }
            inputStream.close();
            out.close();
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/introductionCheck")
    public boolean introductionCheck(HttpServletResponse response,String fileName,String type) throws IOException {
        try {
            Resource resource = new ClassPathResource("/static/"+type+"/"+fileName);
            File sourceFile =  resource.getFile();
            if (!sourceFile.exists()) {
                throw new BizException("文件不存在");
            }
        } catch (Exception e) {
            //e.printStackTrace();
            return false;
        }
        return true;
    }
}
